package org.netwia.networks;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.netwia.utils.ValidationUtils;

/**
 * This implementation of output listener which sends the produced output to the remote network. 
 * 
 * @author radek.hecl
 *
 * @param <T> type of the network parameter
 */
public class RemoteProxyOutputListener<T> implements OutputListener<T> {

    /**
     * Builder object.
     * 
     * @param <T> type of the network parameter
     */
    public static class Builder<T> {

        /**
         * Target network connector.
         */
        private RemoteNetworkConnector<T> target;

        /**
         * Name of the client where the message goes. * for broadcast to all the other clients.
         */
        private String targetClient;
        
        /**
         * Name of the package to send.
         */
        private String packageName;
        
        /**
         * Sets target network connector.
         * 
         * @param target target network connector
         * @return this instance
         */
        public Builder<T> setTarget(RemoteNetworkConnector<T> target) {
            this.target = target;
            return this;
        }
        
        /**
         * Sets target client. This is where the message goes. * for broadcast to all the other clients.
         * 
         * @param targetClient target client, * for broadcast
         * @return this instance
         */
        public Builder<T> setTargetClient(String targetClient) {
            this.targetClient = targetClient;
            return this;
        }
        
        /**
         * Sets name of the package to query for.
         * 
         * @param packageName name of the package to query for
         * @return this instance
         */
        public Builder<T> setPackageName(String packageName) {
            this.packageName = packageName;
            return this;
        }
        
        /**
         * Builds the result object.
         * 
         * @return created object
         */
        public RemoteProxyOutputListener<T> build() {
            return new RemoteProxyOutputListener<T>(this);
        }
    }

    /**
     * Target network connector.
     */
    private RemoteNetworkConnector<T> target;

    /**
     * Name of the client where the message goes. * for broadcast to all the other clients.
     */
    private String targetClient;
    
    /**
     * Name of the package to send.
     */
    private String packageName;

    /**
     * Creates new instance.
     * 
     * @param builder builder object
     */
    public RemoteProxyOutputListener(Builder<T> builder) {
        target = builder.target;
        targetClient = builder.targetClient;
        packageName = builder.packageName;
        guardInvariants();
    }

    /**
     * Guards this object to be consistent. Throws exception if this is not the case.
     */
    private void guardInvariants() {
        ValidationUtils.guardNotNull(target, "target cannot be null");
        ValidationUtils.guardNotEmpty(targetClient, "targetClient cannot be empty");
        ValidationUtils.guardNotEmpty(packageName, "packageName cannot be empty");
    }

    @Override
    public void outputProduced(T output) {
        target.sendOutput(targetClient, packageName, output);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

}
